package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.Natatorium;
import com.example.entity.NatatoriumTypeList;
import com.example.service.NatatoriumService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("natatoriums")
public class NatatoriumController {

    @Autowired
    private NatatoriumService natatoriumService;


    /**
     * 泳池分页查询
     * @return
     */
    @GetMapping("/getPageNatatorium/{current}/{size}")
    public R getPageNatatorium(@PathVariable int current,@PathVariable int size,Natatorium natatorium){


        IPage page =new Page(current,size);
        LambdaQueryWrapper<Natatorium> lqw =new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(natatorium.getName()),Natatorium::getName,natatorium.getName());

        IPage page1 = natatoriumService.page(page,lqw);

        return R.success("泳池分页查询！",page1);
    }

    /**
     * 查询所有泳池
     * @return
     */
    @GetMapping("/getAllNatatorium")
    public R getAllNatatorium(){

        List<Natatorium> list = natatoriumService.list();
        return R.success("查询所有泳池！",list);
    }

    /**
     * 添加泳池
     * @return
     */
    @PostMapping("/addNatatorium")
    public R addNatatorium(@RequestBody Natatorium natatorium){

        LambdaQueryWrapper<Natatorium> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(natatorium.getName()),Natatorium::getName,natatorium.getName());
        //1.查询数据库此泳池是否存在
        Natatorium one = natatoriumService.getOne(lqw);

        //2.不存在在添加
        if (one==null){
            boolean save = natatoriumService.save(natatorium);
            if (save){
                return R.success("添加泳池成功！",null);
            }else {
                return R.error("添加泳池失败！");
            }
        }else {
            //3.存在
            return R.error("泳池存在！添加失败！");
        }
    }

    /**
     * 通过id修改泳池
     * @return
     */
    @PutMapping("/updateNatatorium")
    public R updateNatatorium(@RequestBody Natatorium natatorium){
        boolean b = natatoriumService.updateById(natatorium);
        if (b){
           return R.success("通过id修改泳池成功",natatorium);
        }else {
           return R.error("通过id修改泳池失败！");
        }
    }

    /**
     * 通过id删除泳池
     * @return
     */
    @DeleteMapping("/deleteNatatorium/{id}")
    public R deleteNatatorium(@PathVariable int id){

        boolean b = natatoriumService.removeById(id);
        if (b){
           return R.success("通过id删除泳池成功",id);
        }else {
           return R.error("通过id删除泳池失败！");
        }
    }

    /**
     * 根据id查询泳池信息
     * @return
     */
    @GetMapping("/getById/{id}")
    public R getById(@PathVariable int id){
        Natatorium byId = natatoriumService.getById(id);
        return R.success("根据id查询泳池信息(修改回显)",byId);
    }

    /**
     * 按泳池类型查询
     * @return
     */
    @GetMapping("/getAllNatatoriumByType")
    public R getAllNatatoriumByType(){

        ArrayList<NatatoriumTypeList> arr =new ArrayList<>();

        //查询所有分类
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("DISTINCT type");
        List<Natatorium> list =natatoriumService.list(queryWrapper);


        //根据包含的分类 对包含的分类进行逐一查询对应分类的泳池
        for (Natatorium natatorium:list) {

            LambdaQueryWrapper<Natatorium> lqw2 =new LambdaQueryWrapper<>();
            lqw2.eq(Strings.isNotEmpty(natatorium.getType()),Natatorium::getType,natatorium.getType());
            List<Natatorium> list1 = natatoriumService.list(lqw2);

            NatatoriumTypeList natatoriumTypeList =new NatatoriumTypeList();
            natatoriumTypeList.setType(natatorium.getType());
            natatoriumTypeList.setNatatoriumList(list1);

            arr.add(natatoriumTypeList);
        }
        System.out.println(arr);

        return R.success("按泳池类型查询！",arr);
    }

}

